//
//  NormalViewController.m
//  海角音乐
//
//  Created by dllo on 15/8/8.
//  Copyright (c) 2015年 杨元石. All rights reserved.
//

#import "NormalViewController.h"

@interface NormalViewController ()

@end

@implementation NormalViewController

- (void)viewDidLoad {
    [super viewDidLoad];    
 
    
    CGFloat title_width = SCREEN_WIDTH*0.533333; // 200
    CGFloat title_height = SCREEN_WIDTH*0.08; // 30
    
    self.TitleView = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, title_width, title_height)];
    self.TitleView.textColor = [UIColor whiteColor];
    self.TitleView.textAlignment = NSTextAlignmentCenter;
    self.TitleView.text = self.recommedModel.desc;
    self.navigationItem.titleView = self.TitleView;    
    

    

    _DetailArr = [[NSMutableArray alloc] init];
    _ArrOfmusicID = [[NSMutableArray alloc] init];
    
    // 这里不做自适应
    [self setAutomaticallyAdjustsScrollViewInsets:NO];
    
    // 加上64高
    _normalView = [[NormalView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT)];
    _normalView.tableView.dataSource = self;
    _normalView.tableView.delegate = self;
    _normalView.tableView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:_normalView];
    [_normalView release];
    

    [self getMusicData];
    
    Legacy = [SinglePlayer shareHandleSinglePlayer];
    
    
    // Do any additional setup after loading the view.
}

- (void)getMusicData
{
    
    NSString *type = [self.recommedModel.action objectForKey:@"type"];
    NSString *url_Web = @"";
    
    if ([[type description] isEqualToString:@"1"]) {
        
        self.navigationItem.title = @"浏览";
        url_Web = [self.recommedModel.action objectForKey:@"value"];
        _webView = [[UIWebView alloc] initWithFrame:(CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT-64))];
        _webView.backgroundColor = [UIColor whiteColor];
        [self.view addSubview:_webView];
        NSURL *url = [NSURL URLWithString:url_Web];
        NSURLRequest *request = [NSURLRequest requestWithURL:url];
        [_webView loadRequest:request];
        [_webView release];
        
    } else if ([[type description] isEqualToString:@"5"]) {
        
        // 添加播放器
        BarPlayer = [DanLiPlayer shareHandleDanLiPlayer:CGRectZero];
        [self.view addSubview:BarPlayer];
        HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        HUD.removeFromSuperViewOnHide = YES;
        HUD.labelText = @"正在加载...";
        [self.view addSubview:HUD];
        HUD.mode = MBProgressHUDModeIndeterminate;
        
        // 根据 value 值来判断 是webView 还是 正常的歌单
        // 根据value 值来拼接字符串网址
        NSString *value = [self.recommedModel.action objectForKey:@"value"];
        self.navigationItem.title = self.recommedModel.name;
        [_normalView getURLForImage:self.recommedModel.pic_url textForLabel:self.recommedModel.desc];
        NSString *url_string = @"http://v1.ard.q.itlily.com/share/user_timeline?msg_ids=%5B";
        url_Web = [NSString stringWithFormat:@"%@%@", url_string, value];
        
        
        [AFN urlString:url_Web getAFNdata:^(id responseObject) {
            
            NSArray *dataArr = [responseObject objectForKey:@"data"];
            for (NSDictionary *dic in dataArr) {
                NSArray *songListArr = [dic objectForKey:@"songlist"];
                for (NSDictionary *littleDic in songListArr) {
                    NSString *song_id = [littleDic objectForKey:@"song_id"];
                    [_ArrOfmusicID addObject:song_id];
                }
            }
            NSMutableString *szMuMyString = [NSMutableString stringWithString:@""];
            for (int i = 0; i< _ArrOfmusicID.count; i++) {
                NSString *kk = [_ArrOfmusicID objectAtIndex:i];
                NSString *kkkk = [NSString stringWithFormat:@"%@," ,kk];
                [szMuMyString appendString:kkkk];
            }
            
            NSString *one = @"http://ting.hotchanson.com/songs/downwhite?song_id=";
            
            NSString *url_string = [NSString stringWithFormat:@"%@%@", one, szMuMyString];
            
            [AFN urlString:url_string getAFNdata:^(id responseObject) {
                
                NSArray *dataArr = [responseObject objectForKey:@"data"];
                for (NSDictionary *dic in dataArr) {
                    ReDetailModel *reDetail = [[ReDetailModel alloc] init];
                    [reDetail setValuesForKeysWithDictionary:dic];
                    [_DetailArr addObject:reDetail];
                }
                HUD.hidden = YES;
                [_normalView.tableView reloadData];
            }];
        }];
    }
}

// 下拉效果
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    // 改变垂直偏移量
    CGFloat yy = scrollView.contentOffset.y;
    if (yy <= 0) {
        // 取正值
        yy = 0 - yy;
        _normalView.effectView.frame = CGRectMake(0 - yy , 0 - yy, self.view.frame.size.width + yy + yy, 150 + yy);


    }
}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _DetailArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellId = @"cell";
    
    ReDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    
    if (cell == NO) {
        
        cell = [[ReDetailTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
    }
    
    if (_DetailArr.count > indexPath.row) {
        ReDetailModel *reDic = [_DetailArr objectAtIndex:indexPath.row];
        
        [cell getDataForCell:reDic];
    }
    cell.textLabel.text = [NSString stringWithFormat:@"%ld", indexPath.row+1];
    cell.textLabel.textColor = [UIColor whiteColor];
    
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 80*ADJUSTWIDTH;
}



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    ReDetailModel *reDetailmodel = [_DetailArr objectAtIndex:indexPath.row];
    BarPlayer.reDetailModel = reDetailmodel;
    BarPlayer.dataArr = _DetailArr;
    BarPlayer.index = indexPath.row;
    [self playeMusicWithModel:reDetailmodel index:indexPath.row];
    

}


// 播放方法
- (void)playeMusicWithModel:(ReDetailModel *)model index:(NSInteger)index
{

    
    UIBackgroundTaskIdentifier bgTask = 0;
    
    if([UIApplication sharedApplication].applicationState== UIApplicationStateBackground) {
        
        NSLog(@"后台播放");
        
        if (model.audition_list.count > 1) {
            NSDictionary *dic = [model.audition_list objectAtIndex:1];
            NSString *url_string = [dic objectForKey:@"url"];
            [Legacy play:url_string];
            
        } else {
            NSDictionary *dic = [model.audition_list objectAtIndex:0];
            NSString *url_string = [dic objectForKey:@"url"];
            [Legacy play:url_string];
        }
        
        UIApplication*app = [UIApplication sharedApplication];
        UIBackgroundTaskIdentifier newTask = [app beginBackgroundTaskWithExpirationHandler:nil];
        
        if(bgTask!= UIBackgroundTaskInvalid) {
            [app endBackgroundTask: bgTask];
        }
        bgTask = newTask;
        
    }
    
    else {
        
        NSLog(@"前台播放");
        if (model.audition_list.count > 1) {
            NSDictionary *dic = [model.audition_list objectAtIndex:1];
            NSString *url_string = [dic objectForKey:@"url"];
            [Legacy play:url_string];
            
        } else {
            NSDictionary *dic = [model.audition_list objectAtIndex:0];
            NSString *url_string = [dic objectForKey:@"url"];
            [Legacy play:url_string];
        }
    }
    
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
